#include "DeviceGroup.h"
#include "ui_DeviceGroup.h"

DeviceGroup::DeviceGroup(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeviceGroup),
    devmodel(this)
{
    ui->setupUi(this);
    ui->comboBox->addItem("类型1",0);
    ui->comboBox->addItem("类型2",1);
    ui->comboBox->addItem("类型3",2);
    ui->comboBox->addItem("类型4",3);
     ui->tableView->setModel(&devmodel);
     connect(SnaillTcpClient::instance(), &SnaillTcpClient::dataReceived, this, &DeviceGroup::handleDataReceived);
     void (QComboBox::*currentIndexChangedInt)(int) = &QComboBox::currentIndexChanged;
     connect(ui->comboBox, currentIndexChangedInt, this, &DeviceGroup::boxSelect);
}

DeviceGroup::~DeviceGroup()
{
    delete ui;
}

QList<deviceModel> DeviceGroup::getDeviceList(const QString &jsonString)
{
    QList<deviceModel> deviceList;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
    if (jsonDoc.isObject()) {
        QJsonObject rootObject = jsonDoc.object();
        if (rootObject.contains("data") && rootObject["data"].isArray()) {
            QJsonArray jsonArray = rootObject["data"].toArray();
            for (const QJsonValue &value : jsonArray) {
                if (value.isObject()) {
                    QJsonObject deviceObject = value.toObject();
                    deviceModel device;
                    device.id = deviceObject["id"].toString();
                    device.user_id = deviceObject["user_id"].toString();
                    device.name = deviceObject["name"].toString();
                    device.type = deviceObject["type"].toString();
                    device.location = deviceObject["location"].toString();
                    device.manufacturer = deviceObject["manufacturer"].toString();
                    device.model = deviceObject["model"].toString();
                    device.status = deviceObject["status"].toString();
                    device.installation_date = deviceObject["installation_date"].toString();
                    deviceList.append(device);
                }
            }
        }
    }
    return deviceList;
}

void DeviceGroup::handleDataReceived(const QByteArray &data)
{
    qDebug()<<"==+++=="<<data;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data);
    QJsonObject jsonObj = jsonDoc.object();
    QString action = jsonObj["action"].toString();
    if(action=="deviceGroupList"){
        qDebug()<<"deviceList==="<<jsonObj;

        deviceList =getDeviceList(data);
        devmodel.updateAllDeviceList(deviceList);
    }
}

void DeviceGroup::boxSelect(int index)
{

    qDebug()<<"index==="<<index;
    QJsonObject jsonObj;
    jsonObj["action"] ="deviceGroupList";
    jsonObj["type"] =QString::number(index);
    if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
        //  qDebug() << "deviceList data sent successfully";
    } else {

        return ;
    }
}

void DeviceGroup::showEvent(QShowEvent *event)
{
    qDebug() << "页面显示，进入页面操作可在此处添加";
    QJsonObject jsonObj;
    jsonObj["action"] ="deviceGroupList";
    jsonObj["type"] =QString::number(0 );
    if (SnaillTcpClient::instance()->sendJsonData(jsonObj)) {
        //  qDebug() << "deviceList data sent successfully";
    } else {

        return ;
    }
    QWidget::showEvent(event);
}
